<!-- 帖子管理 -->
<template>
  <div>
    <!-- 模糊搜索表单 -->
    <el-form
      ref="posts"
      :inline="true"
      :model="ruleForm"
      :rules="rule"
      class="demo-form-inline searchUser"
    >
      <el-form-item label="任意字段" prop="keyword">
        <el-input
          v-model="ruleForm.keyword"
          placeholder="请输入任意文字"
        ></el-input>
        <el-button
          type="primary"
          @click="
            findPosts({
              keyword: ruleForm.keyword,
              size: page.size,
            })
          "
          ><i class="el-icon-search"></i><span>搜索</span></el-button
        >
      </el-form-item>
      <el-form-item label="板块名称">
        <el-select
          placeholder="请选择板块"
          v-model="ruleForm.value"
          @change="
            initPlatePosts({
             plate_id:ruleForm.value,
             page:0,
             size:page.size
            })
          "
        >
          <el-option
            v-for="(item, index) in ruleForm.plates"
            :key="index"
            :label="item.topic"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('posts')"
          ><i class="el-icon-refresh"></i><span>重置</span></el-button
        >
      </el-form-item>
    </el-form>
    <!-- 查看文章详情 -->
    <el-dialog
      :title="postsContent.title"
      :visible.sync="centerDialogVisible"
      width="50%"
      center
    >
      <span v-html="postsContent.content"></span>
      <span slot="footer" class="dialog-footer"></span>
    </el-dialog>
    <!-- 信息表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="标题：">
              <span>{{ scope.row.title }}</span>
            </el-form-item>
            <el-form-item label="发布者：">
              <span>{{ scope.row.publisher.username }}</span>
            </el-form-item>
            <el-form-item label="微信：">
              <span v-if="scope.row.wechat_number">{{
                scope.row.wechat_number
              }}</span>
              <span v-else>暂无</span>
            </el-form-item>
            <el-form-item label="所属板块：">
              <span>{{ scope.row.plate.topic }}</span>
            </el-form-item>
            <el-form-item label="发布时间：">
              <span>{{ scope.row.created_time }}</span>
            </el-form-item>
            <el-form-item label="文章状态：">
              <span v-if="scope.row.root">父文章</span>
              <span v-else>子评论</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" min-width="120">
      </el-table-column>
      <el-table-column prop="publisher.username" label="发布者" min-width="130">
      </el-table-column>
      <el-table-column
        prop="plate.topic"
        label="所属板块"
        min-width="130"
      ></el-table-column>
      <el-table-column label="状态" min-width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.root">父文章</span>
          <span v-else>子评论</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="created_time"
        label="发布时间"
        min-width="190"
      ></el-table-column>
      <el-table-column label="操作" min-width="180">
        <template slot-scope="scope">
          <el-row>
            <el-button
              type="text"
              @click="openDialog(true, scope.row.title, scope.row.content)"
              ><i class="el-icon-key"></i><span>查看详情</span></el-button
            >
            <el-button
              type="text"
              @click="deletePost(scope.row.id, scope.row.title)"
              ><i class="el-icon-lock"></i><span>删除</span></el-button
            >
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.number + 1"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        background
        :total="page.total_elements"
        v-if="page.total_elements != 0"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      centerDialogVisible: false,
      postsContent: {},
      rule: {
        keyword: [
          { min: 2, message: "长度在不得小于2个字符", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this["getData"]({
      page: 0,
      size: 10,
    });
    this["getPlate"]({
      page: 0,
      size: 100,
    });
    console.log(this["value"]);
  },
  methods: {
    ...mapActions("adminPosts", [
      "getData",
      "getPlate",
      "initPlatePosts",
      "getTermData",
      "findPosts",
      "removePosts",
    ]),
    handleSizeChange(val) {
      this.$store.commit("adminPosts/HANDLE_SIZE_CHANGE", {
        val,
      });
      this["getTermData"]({
        url: this["url_data"].url,
        params: this["url_data"].params,
      });
    },
    handleCurrentChange(val) {
      this.$store.commit("adminPosts/HANDLE_CURRENT_CHANGE", {
        val,
      });
      this["getTermData"]({
        url: this["url_data"].url,
        params: this["url_data"].params,
      });
    },
    deletePost(post_id, name) {
      this.$confirm(`此操作将永久删除${name}, 是否继续?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let status = await this["removePosts"]({ post_id });
          if (status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            setTimeout(() => {
              this["getTermData"]({
                url: this["url_data"].url,
                params: this["url_data"].params,
              });
            }, 500);
          }
        })
        .catch((error) => {
          this.$message({
            type: "info",
            message: error,
          });
        });
    },
    openDialog(isopen, title, content) {
      this.centerDialogVisible = isopen;
      this.postsContent.title = title;
      this.postsContent.content = content;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$store.commit("adminPosts/RESET_FORM",{key:'value'});
      this["getData"]({
        page: 0,
        size: 10,
      });
    },
  },
  computed: {
    ...mapState("adminPosts", [
      "tableData",
      "page",
      "url_data",
      "ruleForm",
    ]),
  },
};
</script>

<style lang='scss' scoped>
@import "@/assets/css/adminPerson_y.scss";
</style>
